﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace MVCCentral.Framework.Logger
{
    /// <summary>
    /// A static class to implement log management from SimpleLogger.
    /// </summary>
    public class LogManager
    {
        /// <summary>
        /// Log a message to all the listed ILogger subclasses.
        /// </summary>
        /// <param name="args"></param>
        public static void WriteMessage(LogEventArgs args)
        {
            string[] types =
                ConfigurationManager.AppSettings.Get("LogTypes").Split(',');

            List<LoggingStrategy> strategyList = new List<LoggingStrategy>();
            foreach (string type in types)
            {
                LoggingStrategy logType =
                    (LoggingStrategy)Enum.Parse(typeof(LoggingStrategy), type, true);
                strategyList.Add(logType);
            }

            // Implement the factory pattern to return the proper Logger subclasses.
            LogFactory factory = new LogFactory();
            List<ILogger> loggers = factory.CreateLoggers(strategyList);

            int i = 0;
            // Iterate through the list of loggers and log the messages.
            foreach (ILogger logger in loggers)
            {
                ILogger log = logger;

                args.LogType = types[i];

                // Log the message.
                log.Log(args);
                i++;
            }
        }
    }
}
